<?php
/**
 * Created by PhpStorm.
 * User: PGF(592472116@qq.com)
 * Date: 2016/8/14
 * Time: 16:42
 */

namespace Admin\Controller;


class InformationController extends AdminController
{
	public function index(){
		$Information = D('Information');
		$information = $this->lists($Information);
		foreach ($information as &$item){
			$item['thumb'] = D('picture')->Where('id='.$item['thumb'])->getField('path');
		}
		$this->Assign('_list',$information);
		$this->Display();
	}

	/**
	 * 添加便民信息
	 */
	public function add(){
		$title = I('post.title');
		$thumb = I('post.thumb');
		$content = I('post.content');
		if(IS_POST){
			if($title && $thumb && $content){
				D('Information')->add(array(
					'title' => htmlspecialchars($title),
					'thumb' => $thumb,
					'content' => $content,
					'add_time' => time(),
					'update_time' => time(),
				));
				$this->success('添加成功，正在返回！',U('index'));
			}else{
				$this->error('请完成表单');
			}
		}else {
			include 'Ueditor/Ueditor.class.php';
			$ue = new \Ueditor();
			$ue->init ();
			$editor = $ue->editor ( 'content' );
			$this->assign ( 'content', $editor );
			$this->Display ();
		}
	}
	/**
	 * 更新便民信息
	 */
	public function edit(){
		$id = I('get.id');
		if(!$id) {
			$this->error ( '参数有误!', U ( 'index' ) );
			return;
		}
		$info = D('Information')->Where('id='.(int)$id)->find();
		if(!$info){
			$this->error ( '便民信息不存在!', U ( 'index' ) );
			return;
		}
		$info['thumb_c'] = D('picture')->Where('id='.$info['thumb'])->getField('path');
		$title = I('post.title');
		$thumb = I('post.thumb');
		$content = I('post.content');
		if(IS_POST){
			if($title && $thumb && $content){
				D('Information')->Where('id='.$id)->save(array(
					'title' => htmlspecialchars($title),
					'thumb' => $thumb,
					'content' => $content,
					'add_time' => time(),
					'update_time' => time(),
				));
				$this->success('添加成功，正在返回！',U('index'));
			}else{
				$this->error('请完成表单');
			}
		}else {
			include 'Ueditor/Ueditor.class.php';
			$ue = new \Ueditor();
			$ue->init ();
			$this->Assign('info',$info);
			$editor = $ue->editor ( 'content' ,$info['content']);
			$this->assign ( 'content', $editor );
			$this->Display ();
		}
	}

	public function delete ()
	{
		$id = I('id',0);
		if(!$id){
			$this->error('参数有误');
		}else{
			if(D('information')->Where('id='.$id)->delete()){
				$this->success('删除成功！');
			}else{
				$this->error('删除时出现问题！');
			}
		}
	}
}